package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.HerogongMapper;
import com.xgame.server.db.mapper.HeroitemMapper;
import com.xgame.server.db.model.HeroGong;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class HeroGongCsTool {

    @Resource
    private HerogongMapper herogongMapper;

    //插入经验副本
    public Integer insertHeroGong(String account, String playerId, Game.HeroOneCS data) throws Exception {

        List<Game.HeroGongCS> items = data.getGongListList();
        int length = items.size();
        Integer count = new Integer(0);
        for (int i = 0; i < length; i++) {
            Game.HeroGongCS one = items.get(i);
            HeroGong map = new HeroGong();
            map.setAccount(account);
            map.setPlayerId(playerId);
            map.setHeroId(data.getId());
            map.setBookId(one.getBookId());

            String str = "";
            List<Game.AcupointOneCS> ones = one.getOpenListList();
            if (ones.size() > 0) {
                for (int j = 0; j < ones.size(); j++) {
                    if (j == ones.size() - 1) {
                        str = str + ones.get(j).getId() + "," + ones.get(j).getIndex() + "," + ones.get(j).getLevel();
                    } else {
                        str = str + ones.get(j).getId() + "," + ones.get(j).getIndex() + "," + ones.get(j).getLevel() + ";";
                    }
                }
            }

            map.setOpenList(str);
            map.setCurExp(one.getCurExp());
            map.setLevel(one.getLevel());

            str = "";
            List<Integer> twos = one.getSariraListList();
            for (int j = 0; j < twos.size(); j++) {
                if (j == twos.size() - 1) {
                    str = str + twos.get(j);
                } else {
                    str = str + twos.get(j) + ";";
                }
            }
            map.setSariraList(str);
            map.setRate(one.getRate());
            map.setUseCount(one.getUseCount());
            map.setIsUse(one.getIsUse() == true ? 1 : 0);
            map.setOpenYx(one.getOpenYx()== true ? 1 : 0);
            map.setUnlockYx(one.getUnlockYx()== true ? 1 : 0);

            count += herogongMapper.insert(map);
        }

        return count;
    }

    //删除记录
    public Integer deleteHeroGong(String account) throws Exception {
        EntityWrapper<HeroGong> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return herogongMapper.delete(userWrapper);
    }

    //查询记录
    public List<HeroGong> selectHeroItem(String account) throws Exception {
        EntityWrapper<HeroGong> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return herogongMapper.selectList(userWrapper);
    }

    //查询记录
    public List<HeroGong> selectHeroItem(String account, Integer heroid) throws Exception {
        EntityWrapper<HeroGong> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);
        userWrapper.eq("hero_id", heroid);

        return herogongMapper.selectList(userWrapper);
    }

}
